﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace VIFON.DAL
{
    public class DABillDetail
    {
        /// <summary>
        /// Delete billdetails by bill
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        internal List<BillDetail> GetBillDetailList()
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.BillDetails;
                    return temp.Count() > 0 ? temp.ToList() : null;
                }
            }
            catch
            {
                return null;
            }
        }
        internal List<BillDetail> GetBillDetailListByCondition(int BillId)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = from billdetail in db.BillDetails
                               where billdetail.BillId==BillId
                               select billdetail;
                    return temp.Count() > 0 ? temp.ToList() : null;
                }
            }
            catch
            {
                return null;
            }
        }
        internal bool Insert(BillDetail boBillDetail)
        {
            try
            {
                using(var db= new DBVIFONDataContext())
                {
                    db.BillDetails.InsertOnSubmit(boBillDetail);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        internal bool Delete(int id)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temps = db.BillDetails.Where(o => o.BillId == id);
                    if (temps == null)
                        return false;
                    foreach (var obj in temps)
                    {
                        db.BillDetails.DeleteOnSubmit(obj);
                    }
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        internal bool Update(BillDetail boBillDetails)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.BillDetails.FirstOrDefault(o => o.BillId == boBillDetails.BillId && o.ProductId==boBillDetails.ProductId);
                    if (temp == null)
                        return false;
                    temp.BillId = boBillDetails.BillId;
                    temp.ProductId = boBillDetails.ProductId;
                    temp.Amounts = boBillDetails.Amounts;
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
